package com.kylin.module.sub.bussiness;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.criterion.Order;

import com.kylin.common.exception.BusinessException;
import com.kylin.common.paginate.DisplayPage;
import com.kylin.common.paginate.PageManager;
import com.kylin.hibernate.BaseHibernateDAO;
import com.kylin.hibernate.CriteriaInfo;
import com.kylin.module.sub.forms.SubInfoForm;
import com.kylin.module.sub.forms.SubUserForm;
import com.kylin.module.sub.forms.TypeInfoForm;
import com.kylin.module.sub.orm.Subinfo;
import com.kylin.module.sub.orm.Subuser;
import com.kylin.module.sub.orm.Typeinfo;

public class SubBlogic {
	protected static Logger log = Logger.getLogger(SubBlogic.class);
	/**
	 * 添加订阅人信息<===>对应的表是subuser
	 * @param form
	 * @return Subuser<对象>
	 * @throws BusinessException
	 */
	public void addSubUserInfo(SubUserForm form) throws BusinessException{
		log.info("添加新的订阅人信息"+form.getLinkman());
		
		//用户信息
		Subuser user = new Subuser();
		user.setAreaphone(form.getAreaphone());
		user.setCompanyname(form.getCompanyname());
		user.setCountry(form.getCountry());
		user.setCountryname(form.getCountryname());
		user.setCountryphone(form.getCountryphone());
		user.setEmail(form.getEmail());
		user.setFkPositionId(form.getFkPositionId());
		user.setFkpositionname(form.getFkpositionname());
		user.setFkUserId(form.getFkUserId());
		user.setGender(form.getGender());
		user.setLanguage(form.getLanguage());
		user.setLinkman(form.getLinkman());
		user.setPhone(form.getPhone());
		user.setMainbussiness(form.getMainbussiness());
		user.setMainbussinessname(form.getMainbussinessname());
		user.setValidationCode(form.getValidationCode());
		BaseHibernateDAO.save(user);
	}

	
	/**
	 * 添加订阅信息<==>对应的表是subinfo
	 * @param info
	 * @param form
	 * @return Subinfo
	 * @throws BusinessException
	 */
	public Subinfo addSubInfo(SubInfoForm info) throws BusinessException{
		log.info("添加订阅信息表：");
			Subinfo sinfo = new Subinfo();
			List userList = (List)BaseHibernateDAO.findAll(Subuser.class,Order.desc("pkSubuserId"));
			
			Long pkSubuserId = null;
			
			if(userList.size()<=0){
				pkSubuserId = Long.decode("1");
			}else{
				Subuser user = (Subuser)userList.get(0);
				pkSubuserId = user.getPkSubuserId();
			}
			sinfo.setPkSubuserId(pkSubuserId);
			BaseHibernateDAO.save(sinfo);
			return sinfo;
	}
	
	/**
	 * 添加订阅类型信息
	 * @param type
	 * @param info
	 * @param form
	 * @throws BusinessException
	 */
	public void addSubType(TypeInfoForm type,SubInfoForm info) throws BusinessException{
		log.info("添加订阅类型信息："+TypeInfoForm.class.getName());
		Typeinfo typeinfo = new Typeinfo(); 
			typeinfo.setPkTypeinfoId(type.getPkTypeinfoId());
			typeinfo.setPktypeinfoname(type.getPktypeinfoname());
			typeinfo.setFkSubinfoId(this.addSubInfo(info).getPfSubinfoId());
			//typeinfo.setFkDictsId(type.getFkDictsId());
			typeinfo.setFkdicttname(type.getFkdicttname());
			//typeinfo.setFkCatDictId(type.getFkCatDictId());
			typeinfo.setFkcatdictname(type.getFkcatdictname());
			typeinfo.setSubplaceId(type.getSubplaceId());
			typeinfo.setSubplacename(type.getSubplacename());
			//typeinfo.setSubtimeId(type.getSubtimeId());
			typeinfo.setSubtimename(type.getSubtimename());
			//try{
				BaseHibernateDAO.save(typeinfo);
			//}
			//catch(Exception e){
				//System.out.println("这里出错了");
			//}
	}
	
	/**
	 * 查询所有的订阅信息
	 * @param pageSize
	 * @param pageNum
	 * @param cinfo
	 * @return DisplayPage
	 * @throws BusinessException
	 */
	public DisplayPage getSubList(int pageSize, int pageNum, CriteriaInfo cinfo) throws BusinessException {
		log.info("查询所有的订阅信息");
		DisplayPage page = PageManager.getDisplayPage(pageSize, pageNum, cinfo);
		return page;
	}
	
	
	/**
	 * 删除订阅信息
	 * @param id
	 * @throws BusinessException
	 */
	public void delete(Long id) throws BusinessException{
		Typeinfo type = (Typeinfo)BaseHibernateDAO.findById(Typeinfo.class, id);
		BaseHibernateDAO.delete(type);
	}
	
	/**
	 * 查新单条订阅信息的详细信息
	 * @param id
	 * @return Typeinfo
	 * @throws BusinessException
	 */
	public Typeinfo typeFindById(Long id) throws BusinessException{
		Typeinfo type = (Typeinfo)BaseHibernateDAO.findById(Typeinfo.class,id);
		return type;
	}
	
	/**
	 * 返回一个Subuser 对象
	 * @param id
	 * @return Subuser
	 * @throws BusinessException
	 */
	public Subuser userFindById(Long id) throws BusinessException{
		Subuser subUser =(Subuser)BaseHibernateDAO.findById(Subuser.class,id);
		return subUser;
	}
	
	/**
	 * 返回一个Subinfo 对象
	 * @param id
	 * @return Subinfo
	 * @throws BusinessException
	 */
	public Subinfo infoFindById(Long id) throws BusinessException{
		Subinfo info = (Subinfo)BaseHibernateDAO.findById(Subinfo.class,id);
		return info;
	}
}
